/*
  Tên:Huỳnh Nguyễn Ngọc Hân
  MSSV:11370551
  Lớp:DHTH7DLT
  Ngày hoàn thành:24/08/2012
  */
#include "frtacgia.h"
#include "ui_frtacgia.h"

frTacGia::frTacGia(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::frTacGia)
{
    ui->setupUi(this);
    //Khởi tạo
    trangThai=false;
    DB =new MySqlConnection();
    resetTableSelectionEvent();
    //Gán sự kiện
    this->connect(ui->btnThem,SIGNAL(clicked()),this,SLOT(themTacGia()));
    this->connect(ui->btnSua,SIGNAL(clicked()),this,SLOT(suaTacGia()));
    this->connect(ui->btnXoa,SIGNAL(clicked()),this,SLOT(xoaTacGia()));
    this->connect(ui->btnThoat,SIGNAL(clicked()),this,SLOT(Thoat()));
    this->connect(ui->btnTim,SIGNAL(clicked()),this,SLOT(timKiem()));
    ui->tvDSTacGia->selectRow(0);
    doiButton(true);
}

frTacGia::~frTacGia()
{
    delete ui;
}
void frTacGia::resetTableSelectionEvent()
{
    loadDS();
    selectionModel = ui->tvDSTacGia->selectionModel();
    connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(selectRow()));
}

void frTacGia::themTacGia()
{

    doiButton(false);
    ui->txtTen->clear();
    ui->txtDiaChi->clear();
    //Khi thêm trạng thái =true
    trangThai=true;
}

void frTacGia::doiButton(bool b)
{
    ui->btnThem->setEnabled(b);
    ui->tvDSTacGia->setEnabled(b);
    ui->txtTen->setReadOnly(b);
    ui->txtDiaChi->setReadOnly(b);

    if(!b)
    {
        ui->btnSua->setText(trUtf8("Lưu"));
        ui->btnXoa->setText(trUtf8("Không lưu"));
    }
    else

    {
        ui->btnSua->setText(trUtf8("Sửa"));
        ui->btnXoa->setText(trUtf8("Xóa"));

    }

}

void frTacGia::suaTacGia()
{

    //Khi thêm
    bool tl=true;
    if(ui->btnSua->text()==trUtf8("Sửa"))
    {
        doiButton(false);
        return;
    }
    if(ui->txtTen->text().trimmed().isEmpty())
    {
        QMessageBox::information(this,trUtf8("Thông báo"),trUtf8("Vui lòng nhập tên tác giả"));
        return;
    }
    if(trangThai)
    {

        if(DB->KiemTraTrung("TacGia","TenTacGia",ui->txtTen->text().trimmed()))
        {
            QMessageBox::warning(this,trUtf8("Thông báo"),trUtf8("Tên tác giả này đã có trong danh sách"));
            ui->txtTen->setFocus();
            ui->txtTen->selectAll();
            return;
        }

        DB->Open();
        QSqlQuery query(DB->DB);
        query.prepare("INSERT INTO TacGia (TenTacGia,DiaChi) VALUES (:tenTG,:diaChi)");
        query.bindValue(":tenTG",ui->txtTen->text().trimmed());
        query.bindValue(":diaChi",ui->txtDiaChi->text().trimmed());
        tl= query.exec();
        DB->Close();
        if(!tl)
            QMessageBox::information(this,trUtf8("Có lỗi xảy ra"),query.lastError().text());
        else
            resetTableSelectionEvent();

        //Gán lại trạng thái
        trangThai=false;

    }
    else
    {
        //Sửa tác giả

        DB->Open();
        QSqlQuery query(DB->DB);
        query.prepare("UPDATE TacGia set TenTacGia=:tenTG,DiaChi=:diaChi WHERE MaTacGia=:maTG");
        query.bindValue(":tenTG",ui->txtTen->text().trimmed());
        query.bindValue(":diaChi",ui->txtDiaChi->text().trimmed());
        query.bindValue(":maTG",m_MaTG);
        tl= query.exec();
        DB->Close();
        if(!tl)
            QMessageBox::information(this,trUtf8("Có lỗi xảy ra"),query.lastError().text());
        else
            {
            resetTableSelectionEvent();
            ui->tvDSTacGia->selectRow(m_rowIndex);
            }
    }

    doiButton(true);
}

void frTacGia::xoaTacGia()
{
    if(ui->btnXoa->text()==trUtf8("Không lưu"))
    {
        doiButton(true);
        selectRow();
        return;
    }
     bool tl=true;
     QString msg=trUtf8("Bạn muốn xóa tác giả '")+ui->txtTen->text()+trUtf8("'\nra khỏi danh sách không ?");
    if(QMessageBox::information(this,trUtf8("Thông báo"),msg,QMessageBox::Yes,QMessageBox::No)==QMessageBox::Yes)
    {
        DB->Open();
        QSqlQuery query(DB->DB);
        query.prepare("DELETE FROM TacGia WHERE id=:maTG");
        query.bindValue(":maTG",m_MaTG);
        tl= query.exec();
        DB->Close();
        if(!tl)
            QMessageBox::information(this,trUtf8("Có lỗi xảy ra"),query.lastError().text());
        else
        {
            resetTableSelectionEvent();
            if(m_MaTG>0)
                m_rowIndex--;
            ui->tvDSTacGia->selectRow(m_rowIndex);
            selectRow();
        }
    }


}

void frTacGia::Thoat()
{
    this->close();
}
void frTacGia::loadDS()
{

    QSqlQueryModel *model = DB->getTable("select * from TacGia");
    model->setHeaderData(0, Qt::Horizontal,trUtf8("Mã"));
    model->setHeaderData(1, Qt::Horizontal,trUtf8("Tên TG"));
    model->setHeaderData(2, Qt::Horizontal,trUtf8("Địa chỉ"));
    ui->tvDSTacGia->setModel(model);
    ui->tvDSTacGia->show();




}
void frTacGia::selectRow()
{
    QAbstractItemModel *item =ui->tvDSTacGia->model();
    QModelIndex index = ui->tvDSTacGia->currentIndex();
    m_rowIndex = index.row();
    ui->txtTen->setText(item->data(item->index(m_rowIndex, 1)).toString());
    ui->txtDiaChi->setText(item->data(item->index(m_rowIndex, 2)).toString());
    m_MaTG=item->data(item->index(m_rowIndex,0)).toInt();

}
void frTacGia::timKiem()
{

    if(ui->btnTim->text()==trUtf8("Tìm"))
    {
        QString sql="select * from TacGia where  MaTacGia like '%"+ui->txtTim->text().trimmed()+"%' or TenTacGia like '%"+ui->txtTim->text().trimmed()+"%'";
        QSqlQueryModel *model = DB->getTable(sql);
        model->setHeaderData(0, Qt::Horizontal,trUtf8("Mã"));
        model->setHeaderData(1, Qt::Horizontal,trUtf8("Tên TG"));
        model->setHeaderData(2, Qt::Horizontal,trUtf8("Địa chỉ"));
        ui->tvDSTacGia->setModel(model);
        ui->tvDSTacGia->show();
        if(!ui->txtTim->text().trimmed().isEmpty())
        ui->btnTim->setText(trUtf8("Thôi"));
    }
    else
    {
        loadDS();
        ui->btnTim->setText(trUtf8("Tìm"));
        ui->txtTim->clear();
    }

}
